{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab 18 - Cental Limit Theorem (CLT)\n",
"\n",
"The Cental Limit Theorem says that if we take many samples from a population, then the means of those samples have a normal distribution. As we take more samples, the mean of the normal distribution gets closer to the mean of the population, and the standard deviation of the normal distribution gets closer to standard deviation of the population divided by the square root of the sample size.\n",
"\n",
"We will experimentally test the Central Limit Theorem in this lab. \n",
"\n",
"Download the CSV file of nuitritional information for Starbucks drinks, which is taken from [Kaggle.com](https://www.kaggle.com/starbucks/starbucks-menu). \n",
"\n",
"First, let's import the necessary libraries."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading and cleaning the data.\n",
"\n",
"Read the CSV file into a dataframe called `drinks`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display your `drinks` dataframe below. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What symbol represents missing data in this data set? Since this is not a standard symbol, Pandas, does not recognize it as meaning missing data. \n",
"\n",
"Pandas does not recognize `-` as missing data here, so we to pass this information to the `read_csv` function by adding the parameter `na_values = \"-\"`. Try reading in the file again, using this parameter below. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display your revised dataframe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How does Pandas represent missing data? \n",
"\n",
"There is one more problem with how the data was read in. What is the first column?\n",
"\n",
"The drink name is really what identifies each row, and should be the row name, not stored in a column. We can do this by adding the parameter `index_col = 0`, which tells `read_csv` that the first column, numbered 0, is the indexes, or row names. \n",
"\n",
"Read in the file one more time below adding both parameters. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display your revised dataframe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do you notice any other possible issues? \n",
"\n",
"There is no data for some of the rows (all columns are `NaN`). We can remove these rows from the dataframe with the code `drinks = drinks.dropna(axis = 0)`. Try it below and display your new dataframe. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing the Central Limit Theorem: Calories column\n",
"\n",
"Plot a histogram of the calorie column data, to visualize its distribution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What do you notice about this distribution?\n",
"\n",
"Now we will take 10,000 random samples of size 30 from the calorie data, compute the mean of each sample, and plot the means as a histogram. We are visualizing the *sampling distribution of the mean* (Lab 11).\n",
"\n",
"The pseudo-code to compute the means and store them in a list is:\n",
"\n",
"create an empty list\n",
"repeat 10,000 times:\n",
" take a sample of size 30 from the drinks dataframe\n",
" compute the mean number of calories for this sample\n",
" add this mean to your list\n",
"
\n",
"\n",
"Write the Python code below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the means as a histogram. Remember to convert your list into a Pandas series and adjust the number of bins to get a good visualization."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What distribution does this look like?\n",
"\n",
"Now we'll simulate the sampling distribution of the mean again, but using a sample size of 50.\n",
"\n",
"For reference, the pseudo-code to compute the means and store them in a list is:\n",
"\n",
"create an empty list\n",
"repeat 10,000 times:\n",
" take a sample of size 50 from the drinks dataframe\n",
" compute the mean number of calories for this sample\n",
" add this mean to your list\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next plot the histogram of these means:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To better compare the two histograms, let's plot them on the same graph, using the `density = True` parameter for each histogram."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens to the distribution as the sample size increases? How does the mean change? How does the standard deviation change? Does this make sense?\n",
"\n",
"The Central Limit Theorem tells us that the mean of the sampling distribution should be close to the mean of the population. Let's test this by computing:\n",
"\n",
"1. the mean of the population (the mean of the calorie column in the `drinks` dataframe)\n",
"2. the mean of the means of the samples of size 30\n",
"3. the mean of the means of the samples of size 50"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Central Limit Theorem also tells us that when the sample size is large:\n",
"\n",
"$\\text{SD of all possible sample means} = \\frac{\\text{population SD}}{\\sqrt{\\text{sample size}}}$ \n",
"\n",
"Let's test this for the size 30 samples. The left hand side of the equation is the standard deviation (SD) of these sample means:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The right hand side of the equation is the standard deviation of the population (calories of all drinks), divided by $\\sqrt{\\text{sample size}}$, which we can calculate with the code `np.sqrt(30)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How similar are the two numbers? This relationship becomes more accurate as the sample size improves. For example, if we took samples (with replacement) of size 1000, these two numbers will be equal to the first decimal point.\n",
"\n",
"### Testing the Cental Limit Theorem: Protein column\n",
"\n",
"Let's repeat this experiment for another variable. Plot a histogram of the amount of protein in the drinks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's take 10,000 samples, each of size 1,000, and compute their means. Use the parameter `replace = True` to sample from the `drinks` dataframe with replacement.\n",
"\n",
"The pseudo-code to compute the means and store them in a list is:\n",
"\n",
"create an empty list\n",
"repeat 10,000 times:\n",
" take a sample of size 1000 with replacement from the drinks dataframe\n",
" compute the mean number of calories for this sample\n",
" add this mean to your list\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot a histogram of the means of the samples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What does the distribution look like?\n",
"\n",
"Let's compare the mean and standard deviation with what is predicted by the Central Limit Theorem.\n",
"\n",
"To compare the means, compute\n",
"1. the mean of your sample means\n",
"2. the mean of the population (protein column in the `drinks` dataframe)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How do the two means compare?\n",
"\n",
"To compare the standard devatiations, compute:\n",
"1. the standard deviation of your sample mean\n",
"2. the standard deviation of the population, divided by the square root of the sample size (1000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How do the two standard deviations compare? \n",
"\n",
"### Challenges:\n",
"- What does the sampling distribution of the mean of the `Carb. (g)` column look like when the sample size is 30, 300, and 900?\n",
"- How does the mean of the means of the samples and the population mean compare for the `Carb. (g)` column when the sample size is 30, 300, and 900?\n",
"- How does the standard deviation of the means of the samples and the population standard deviation compare for the `Carb. (g)` column when the sample size is 30, 300, and 900?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}